# coding=utf-8

# author: Reggie
# time:   2019/08/26 18:02

import os, sys, time
import pika


def Main():
    credentials = pika.PlainCredentials("wangshuliang", "123")
    parameters = pika.ConnectionParameters(host="129.28.181.19",
                                           virtual_host='/',
                                           credentials=credentials)
    connection = pika.BlockingConnection(parameters)  # 连接 RabbitMQ

    channel = connection.channel()  # 创建频道

    # queue = channel.queue_declare(queue='queuetest')  # 声明或创建队列
    while True:  # 循环向队列中发送信息
        message = time.strftime('%H:%M:%S', time.localtime())
        channel.basic_publish(exchange='myhost',
                              routing_key='myhost',
                              body=message,
                              properties=pika.BasicProperties(delivery_mode=2))

        print('send message: %s' % message)

        while True:
            # 检查队列，以重新得到消息计数
            queue = channel.queue_declare(queue='myhost', passive=True)
            messageCount = queue.method.message_count
            print('messageCount: %d' % messageCount)
            if messageCount < 100000:
                break
            # time.sleep(1)
    # 关闭连接
    connection.close()


if __name__ == '__main__':
    Main()
